#include <math.h>
#include <limits.h>

#import <UIKit/UIHardware.h>
#import <Foundation/NSUserDefaults.h>

#import "MouseView.h"

double diffLength(const CGPoint *a, const CGPoint *b)
{
	return sqrt(pow(a->x - b->x, 2.0) + pow(a->y - b->y, 2.0));
}

@implementation MouseView

- (void)dumpGSEvent: (GSEventRef)event
{
	/*
		http://www.cocoadev.com/index.pl?GraphicsServices
	*/
	NSLog(@"GSEventIsChordingHandEvent: %d", GSEventIsChordingHandEvent(event));
	NSLog(@"GSEventGetClickCount: %d", GSEventGetClickCount(event));
	CGRect rect = GSEventGetLocationInWindow(event);
	NSLog(@"GSEventGetLocationInWindow: (%f, %f, %f, %f)", rect.origin.x, rect.origin.y, rect.size.width, rect.size.height);
	NSLog(@"GSEventGetDeltaX: %f", GSEventGetDeltaX(event));
	NSLog(@"GSEventGetDeltaY: %f", GSEventGetDeltaY(event));
	CGPoint inner = GSEventGetInnerMostPathPosition(event);
	NSLog(@"GSEventGetInnerMostPathPosition: (%f, %f)", inner.x, inner.y);
	CGPoint outer = GSEventGetOuterMostPathPosition(event);
	NSLog(@"GSEventGetOuterMostPathPosition: (%f, %f)", outer.x, outer.y);
	NSLog(@"GSEventGetSubType: %d", GSEventGetSubType(event));
	NSLog(@"GSEventGetType: %d", GSEventGetType(event));
	NSLog(@"GSEventDeviceOrientation: %d", GSEventDeviceOrientation(event));
}

- (id)initWithFrame: (CGRect)frame
{
	self = [super initWithFrame: frame];
	
	[self setTapDelegate: self];
	
	mouseDelegate = nil;
	
	prevPoint = prevP1 = prevP2 = CGPointZero;
	
	return self;
}

- (void)setMouseDelegate: (id)delegate
{
	mouseDelegate = delegate;
}

- (BOOL)canHandleGestures
{
	return YES;
}

- (BOOL)canHandleSwipes
{
	return YES;
}

- (void)gestureStarted: (GSEventRef)event
{
	gesStart1 = GSEventGetInnerMostPathPosition(event);
	gesStart2 = GSEventGetInnerMostPathPosition(event);
	gesStartTime = time(NULL);
}

- (void)gestureEnded: (GSEventRef)event
{
	gesEnd1 = GSEventGetInnerMostPathPosition(event);
	gesEnd2 = GSEventGetInnerMostPathPosition(event);
	gesEndTime = time(NULL);
	
	// 1秒以上指を置いていると無効
	if (1 < difftime(gesEndTime, gesStartTime))
		return;
	
	// 開始位置と終了位置が大きくずれている時は無効
	if (32.0 < diffLength(&gesStart1, &gesEnd1) || 32.0 < diffLength(&gesStart2, &gesEnd2))
		return;
	
	[mouseDelegate mouseRightButtonClick];
}

- (void)gestureChanged: (GSEventRef)event
{
/*
	CGPoint p1 = GSEventGetInnerMostPathPosition(event);
	CGPoint p2 = GSEventGetInnerMostPathPosition(event);
	
	
	// 1本目の指を動かさない時だけスクロール
	if (32.0 < diffLength(&gesStart1, &p1))
		return;
	
	// 動きが小さすぎる時は無効
	if (p2.y - prevP2.y < 1)
		return;
	
	[mouseDelegate mouseWheel: (int)(p2.y - prevP2.y)];
	
	prevP1 = p1;
	prevP2 = p2;
*/
}

- (int)swipe: (int)orientation withEvent: (GSEventRef)event
{
	
}

- (void)view: (UIView *)view handleTapWithCount:(int)count event:(GSEventRef)event fingerCount: (int)finger
{
	[mouseDelegate mouseLeftButtonClick];
}

- (void)mouseDragged: (GSEventRef)event
{
	CGSize screen = [UIHardware fullScreenApplicationContentRect].size;
	CGPoint p = GSEventGetLocationInWindow(event).origin;
	
	int margin = [[NSUserDefaults standardUserDefaults] integerForKey: @"margin"];
	
	int x = (int)(p.x / screen.width * USHRT_MAX);
	int y = (int)(p.y / screen.height * USHRT_MAX);
	
	[mouseDelegate mouseMoveAbsolute: CGPointMake(x, y)];
	[mouseDelegate mouseMoveRelative: CGPointMake(p.x - prevPoint.x, p.y - prevPoint.y)];
	
	prevPoint = p;
}

- (void)mouseDown: (GSEventRef)event
{
	prevPoint = GSEventGetLocationInWindow(event).origin;
}

- (void)mouseUp: (GSEventRef)event
{
	
}

@end
